using System;
using ETModel;
using System.Linq;
using UnityEngine;

namespace ETHotfix
{
    public static class MapHelper
    {
        public static async ETVoid EnterMapAsync()
        {
            try {

                //创建Model层的session,指向Gate服务器(例子中就不走realm服务器了)
                ETModel.Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
                ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateSession;

                // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
                Game.Scene.AddComponent<SessionComponent>().Session = ComponentFactory.Create<Session, ETModel.Session>(gateSession);


                G2C_Connect connectResult = (G2C_Connect)await SessionComponent.Instance.Session.Call(new C2G_Connect());
                if (connectResult.Error != 0) {
                    Log.Error(connectResult.Message);
                    return;
                }

                Log.Info("连接成功!");

                // 创建Player
                Player player = ComponentFactory.CreateWithId<Player>(connectResult.PlayerId);
                PlayersComponent playerComponent = Game.Scene.GetComponent<PlayersComponent>();
                playerComponent.MyPlayer = player;


                // 加载Unit资源
                ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
                await resourcesComponent.LoadBundleAsync($"unit.unity3d");

                // 加载场景资源
                await ETModel.Game.Scene.GetComponent<ResourcesComponent>().LoadBundleAsync("map.unity3d");
                // 切换到map场景
                using (SceneChangeComponent sceneChangeComponent = ETModel.Game.Scene.AddComponent<SceneChangeComponent>())
                {
                    await sceneChangeComponent.ChangeSceneAsync(SceneType.Map);
                }

                GameUnitFactory.LoadUnitsByInfos(connectResult.Units);

                var myUnits = connectResult.SelfUnitIds.ToList();
                var myUnitId = myUnits.FirstOrDefault();//只取第一个作为控制单位
                var myUnit = Game.Scene.GetComponent<UnitsComponent>().Get(myUnitId);
                PlayersComponent.Instance.MyPlayer.UnitIds = myUnits;
                PlayersComponent.Instance.MyUnit = myUnit;


                //进入地图后,加入相机
                var cameraComp = Game.Scene.AddComponent<FreeViewUnitCameraComponent>();
                cameraComp.FollowUnit = myUnit;
                Game.Scene.AddComponent<OperaComponent>().OperaUnit = myUnit;


                //调试信息
                foreach (var item in connectResult.AllGizmosMeshs) {
                    GizmosDebug.Instance.AddUnitMesh(item.Key, item.GetVertices(), item.GetIndices(), item.GetCenterPosition(), item.GetRotation(), item.GetScale());
                }
                foreach (var item in connectResult.AllRayTestLine) {
                    GizmosDebug.Instance.AddUnitRayTest(item.Key, new Vector3(item.StartX, item.StartY, item.StartZ), new Vector3(item.EndX, item.EndY, item.EndZ)
                        , new Color(item.ColorR, item.ColorG, item.ColorB));
                }

                Game.EventSystem.Run(HotfixEventIdType.EnterMapFinish);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }	
        }
    }
}